﻿using System;
using System.Text.RegularExpressions;

namespace YJKArch.GlobleSetModule
{
	internal class ArabicNumeralsConvertChineseDigitalKit
	{
		public static string NumToChn(string num)
		{
			if (num.Length == 0)
			{
				return "";
			}
			string text = "";
			if (num[0] == '-')
			{
				text = "负";
				num = num.Remove(0, 1);
			}
			if (num[0].ToString() == ".")
			{
				num = "0" + num;
			}
			if (num[num.Length - 1].ToString() == ".")
			{
				num = num.Remove(num.Length - 1, 1);
			}
			if (num.IndexOf(".") > -1)
			{
				text = text + ArabicNumeralsConvertChineseDigitalKit.ConvertInteger(num.Substring(0, num.IndexOf("."))) + "点" + ArabicNumeralsConvertChineseDigitalKit.ConvertDecimal(num.Substring(num.IndexOf(".") + 1));
			}
			else
			{
				text += ArabicNumeralsConvertChineseDigitalKit.ConvertInteger(num);
			}
			return text;
		}

		public static int ChnToNum(string cnum)
		{
			cnum = Regex.Replace(cnum, "\\s+", "");
			int num = 1;
			int num2 = 1;
			int num3 = 0;
			for (int i = cnum.Length - 1; i > -1; i--)
			{
				int num4 = ArabicNumeralsConvertChineseDigitalKit.CharToUnit(cnum[i]);
				if (num4 > num)
				{
					num = num4;
					num2 = 1;
					if (i == 0)
					{
						num3 += num * num2;
					}
				}
				else if (num4 > num2)
				{
					num2 = num4;
				}
				else
				{
					num3 += num * num2 * ArabicNumeralsConvertChineseDigitalKit.CharToNumber(cnum[i]);
				}
			}
			return num3;
		}

		protected static int CharToNumber(char c)
		{
			if (c <= '二')
			{
				if (c <= '七')
				{
					if (c == '一')
					{
						return 1;
					}
					if (c == '七')
					{
						return 7;
					}
				}
				else
				{
					if (c == '三')
					{
						return 3;
					}
					if (c == '九')
					{
						return 9;
					}
					if (c == '二')
					{
						return 2;
					}
				}
			}
			else if (c <= '八')
			{
				if (c == '五')
				{
					return 5;
				}
				if (c == '八')
				{
					return 8;
				}
			}
			else
			{
				if (c == '六')
				{
					return 6;
				}
				if (c == '四')
				{
					return 4;
				}
				if (c == '零')
				{
					return 0;
				}
			}
			return -1;
		}

		protected static int CharToUnit(char c)
		{
			if (c <= '亿')
			{
				if (c == '万')
				{
					return 10000;
				}
				if (c == '亿')
				{
					return 100000000;
				}
			}
			else
			{
				if (c == '十')
				{
					return 10;
				}
				if (c == '千')
				{
					return 1000;
				}
				if (c == '百')
				{
					return 100;
				}
			}
			return 1;
		}

		private static char ConvertNumber(char x)
		{
			string text = "零一二三四五六七八九";
			string text2 = "0123456789";
			return text[text2.IndexOf(x)];
		}

		private static string ConvertLessThanAMillion(string x)
		{
			string[] array = new string[]
			{
				"",
				"十",
				"百",
				"千"
			};
			string text = "";
			int i;
			for (i = x.Length - 1; i >= 0; i--)
			{
				if (x[i] == '0')
				{
					text = ArabicNumeralsConvertChineseDigitalKit.ConvertNumber(x[i]).ToString() + text;
				}
				else
				{
					text = ArabicNumeralsConvertChineseDigitalKit.ConvertNumber(x[i]).ToString() + array[x.Length - 1 - i] + text;
				}
			}
			while ((i = text.IndexOf("零零")) != -1)
			{
				text = text.Remove(i, 1);
			}
			if (text[text.Length - 1] == '零' && text.Length > 1)
			{
				text = text.Remove(text.Length - 1, 1);
			}
			if (text.Length >= 2 && text.Substring(0, 2) == "一十")
			{
				text = text.Remove(0, 1);
			}
			return text;
		}

		private static string ConvertInteger(string x)
		{
			int length = x.Length;
			string text;
			if (length <= 4)
			{
				text = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x);
			}
			else if (length <= 8)
			{
				text = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x.Substring(0, length - 4)) + "万";
				string text2 = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x.Substring(length - 4, 4));
				if (text2.IndexOf("千") == -1 && text2 != "")
				{
					text = text + "零" + text2;
				}
				else
				{
					text += text2;
				}
			}
			else
			{
				text = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x.Substring(0, length - 8)) + "亿";
				string text2 = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x.Substring(length - 8, 4));
				if (text2.IndexOf("千") == -1 && text2 != "")
				{
					text = text + "零" + text2;
				}
				else
				{
					text += text2;
				}
				text += "万";
				text2 = ArabicNumeralsConvertChineseDigitalKit.ConvertLessThanAMillion(x.Substring(length - 4, 4));
				if (text2.IndexOf("千") == -1 && text2 != "")
				{
					text = text + "零" + text2;
				}
				else
				{
					text += text2;
				}
			}
			int num;
			if ((num = text.IndexOf("零万")) != -1)
			{
				text = text.Remove(num + 1, 1);
			}
			while ((num = text.IndexOf("零零")) != -1)
			{
				text = text.Remove(num, 1);
			}
			if (text[text.Length - 1] == '零' && text.Length > 1)
			{
				text = text.Remove(text.Length - 1, 1);
			}
			return text;
		}

		private static string ConvertDecimal(string x)
		{
			string text = "";
			for (int i = 0; i < x.Length; i++)
			{
				text += ArabicNumeralsConvertChineseDigitalKit.ConvertNumber(x[i]).ToString();
			}
			return text;
		}
	}
}
